<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="LVS," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="一、实验环境需求&amp;amp;准备&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;这次实验要完成的一个架构如下图所示，通过LVS-DR-MASTER,LVS-DR-BACKUP作为LVS负载均衡调度器，并且两者之间通过keepalived来两者之间的HA。keepalived本身就是为了LVS为开发的，所以说通过keepalived来进行LVS的配置就显">
<meta name="keywords" content="LVS">
<meta property="og:type" content="article">
<meta property="og:title" content="LVS原理详解及部署之五：LVS+keepalived实现负载均衡&amp;高可用">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/LVS/12. LVS原理详解及部署之五：LVS+keepalived实现负载均衡&高可用/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="一、实验环境需求&amp;amp;准备&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;这次实验要完成的一个架构如下图所示，通过LVS-DR-MASTER,LVS-DR-BACKUP作为LVS负载均衡调度器，并且两者之间通过keepalived来两者之间的HA。keepalived本身就是为了LVS为开发的，所以说通过keepalived来进行LVS的配置就显">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E5%8E%9F%E7%90%86%E8%AF%A6%E8%A7%A3%E5%8F%8A%E9%83%A8%E7%BD%B2%E4%B9%8B%E4%BA%94%EF%BC%9ALVS+keepalived%E5%AE%9E%E7%8E%B0%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1&%E9%AB%98%E5%8F%AF%E7%94%A8/01.jpeg?raw=true">
<meta property="og:updated_time" content="2017-09-01T08:52:32.123Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="LVS原理详解及部署之五：LVS+keepalived实现负载均衡&amp;高可用">
<meta name="twitter:description" content="一、实验环境需求&amp;amp;准备&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;这次实验要完成的一个架构如下图所示，通过LVS-DR-MASTER,LVS-DR-BACKUP作为LVS负载均衡调度器，并且两者之间通过keepalived来两者之间的HA。keepalived本身就是为了LVS为开发的，所以说通过keepalived来进行LVS的配置就显">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/LVS%E5%8E%9F%E7%90%86%E8%AF%A6%E8%A7%A3%E5%8F%8A%E9%83%A8%E7%BD%B2%E4%B9%8B%E4%BA%94%EF%BC%9ALVS+keepalived%E5%AE%9E%E7%8E%B0%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1&%E9%AB%98%E5%8F%AF%E7%94%A8/01.jpeg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/LVS/12. LVS原理详解及部署之五：LVS+keepalived实现负载均衡&高可用/"/>





  <title>LVS原理详解及部署之五：LVS+keepalived实现负载均衡&高可用 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/LVS/12. LVS原理详解及部署之五：LVS+keepalived实现负载均衡&高可用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">LVS原理详解及部署之五：LVS+keepalived实现负载均衡&高可用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:46+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一、实验环境需求-amp-准备"><a href="#一、实验环境需求-amp-准备" class="headerlink" title="一、实验环境需求&amp;准备"></a>一、实验环境需求&amp;准备</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这次实验要完成的一个架构如下图所示，通过LVS-DR-MASTER,LVS-DR-BACKUP作为LVS负载均衡调度器，并且两者之间通过keepalived来两者之间的HA。keepalived本身就是为了LVS为开发的，所以说通过keepalived来进行LVS的配置就显得十分的方便。而且keepalived是直接操作ip_vs不用通过ipvsadm，所以更加方便。</p>
<ol>
<li>实验架构图&amp;需求表：</li>
</ol>
<p><img src="https://github.com/hcldirgit/image/blob/master/LVS%E5%8E%9F%E7%90%86%E8%AF%A6%E8%A7%A3%E5%8F%8A%E9%83%A8%E7%BD%B2%E4%B9%8B%E4%BA%94%EF%BC%9ALVS+keepalived%E5%AE%9E%E7%8E%B0%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1&amp;%E9%AB%98%E5%8F%AF%E7%94%A8/01.jpeg?raw=true" alt=""></p>
<table>
<thead>
<tr>
<th>角色</th>
<th>IP地址</th>
<th>备注</th>
</tr>
</thead>
<tbody>
<tr>
<td>主LVS调度器（MASTER)</td>
<td>192.168.41.181</td>
<td>使用keepalived配置备</td>
</tr>
<tr>
<td>LVS调度器（BACKUP)</td>
<td>192.168.41.25</td>
<td>&#160;</td>
</tr>
<tr>
<td>1HTTP服务器（RS1)</td>
<td>192.168.41.31</td>
<td>apache服务器（一般生产环境需要外网IP地址，这里用内网IP地址替代）</td>
</tr>
<tr>
<td>HTTP服务器（RS2)</td>
<td>192.168.41.33</td>
<td>&#160;</td>
</tr>
<tr>
<td>虚拟IP地址（VIP)</td>
<td>192.168.41.249</td>
<td>虚拟IP地址</td>
</tr>
</tbody>
</table>
<ol>
<li>部署http服务器，验证能正常访问</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里就是要保证http能正常访问。</p>
<h2 id="二、配置keepalived实现负载均衡-amp-高可用"><a href="#二、配置keepalived实现负载均衡-amp-高可用" class="headerlink" title="二、配置keepalived实现负载均衡&amp;高可用"></a>二、配置keepalived实现负载均衡&amp;高可用</h2><h3 id="安装keepalived软件"><a href="#安装keepalived软件" class="headerlink" title="安装keepalived软件"></a>安装keepalived软件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz</div><div class="line">tar -zxf keepalived-1.2.8.tar.gz</div><div class="line"><span class="built_in">cd</span> keepalived-1.2.8</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/keepalived</div><div class="line">make</div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置keepalived的自启动&amp;拷贝keepalived的执行程序</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">cp /usr/<span class="built_in">local</span>/keepalived/sbin/keepalived /usr/sbin/</div><div class="line">cp /usr/<span class="built_in">local</span>/keepalived/sbin/keepalived /usr/sbin/</div><div class="line">cp /usr/<span class="built_in">local</span>/keepalived/etc/sysconfig/keepalived /etc/sysconfig/</div><div class="line">cp /usr/<span class="built_in">local</span>/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开启内核的转发功能</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /etc/sysctl  net.ipv4.ip_forword = 1</div></pre></td></tr></table></figure>
<h3 id="配置LVS-DR-MASK的keepalived-conf配置文件"><a href="#配置LVS-DR-MASK的keepalived-conf配置文件" class="headerlink" title="配置LVS-DR-MASK的keepalived.conf配置文件"></a>配置LVS-DR-MASK的keepalived.conf配置文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line">! Configuration File <span class="keyword">for</span> keepalived</div><div class="line">global_defs &#123;</div><div class="line"> notification_email &#123;</div><div class="line"> 752119102@qq.com <span class="comment">#设置报警邮箱，一般不再这做，而是用其他方式报警。</span></div><div class="line"> &#125;</div><div class="line"> notification_email_from keepalived@localhost <span class="comment">#设定发送邮件地址</span></div><div class="line"> smtp_server 127.0.0.1 <span class="comment">#设定发送邮件服务器</span></div><div class="line"> smtp_connect_timeout 30 <span class="comment">#设定SMTP连接超时时间</span></div><div class="line"> router_id LVS_DEVEL <span class="comment">#查阅说明文档得知route_id配置是为了标识当前节点，我将其设置为NodeA。当然两个节点的此项设置可相同，也可不相同。</span></div><div class="line">&#125;</div><div class="line">vrrp_instance VI_1 &#123; <span class="comment">#定义虚拟路由实例，不同实例ID不同。</span></div><div class="line"> state MASTER  <span class="comment">#定义服务器在keepalived中的角色主服务器</span></div><div class="line"> interface eth0 <span class="comment">#定义进行检测的端口eth0</span></div><div class="line"> virtual_router_id 51 <span class="comment">#定义虚拟路由ID，同一个实例的主从一样。</span></div><div class="line"> priority 100  <span class="comment">#定义在虚拟路由器组的权限，越大越高</span></div><div class="line"> advert_int 1 <span class="comment">#定义检测时间间隔</span></div><div class="line"> authentication &#123; <span class="comment">#定义认证方式密码，主从必须一样</span></div><div class="line"> auth_type PASS</div><div class="line"> auth_pass 1111</div><div class="line"> &#125;</div><div class="line"> virtual_ipaddress &#123; <span class="comment">#指定虚拟IP地址</span></div><div class="line"> 192.168.41.249</div><div class="line"> &#125;</div><div class="line">&#125;</div><div class="line">virtual_server 192.168.41.249 80 &#123; <span class="comment">#定义虚拟服务，需指定IP地址和端口，空格隔开。</span></div><div class="line"> delay_loop 6  <span class="comment">#定义RS运行情况监测时间间隔</span></div><div class="line"> lb_algo rr  <span class="comment">#定义负载调度算法</span></div><div class="line"> lb_kind DR <span class="comment">#定义LVS的工作模式</span></div><div class="line"> nat_mask 255.255.255.0 <span class="comment">#定义虚拟服务的mask</span></div><div class="line"> persistence_timeout 50 <span class="comment">#定义会话保持时间，S为单位</span></div><div class="line"> protocol TCP <span class="comment">#指定转发协议</span></div><div class="line"> real_server 192.168.41.31 80 &#123; <span class="comment">#定义真实服务器IP地址和端口</span></div><div class="line"> weight 1 <span class="comment">#定义RS的权重</span></div><div class="line"> TCP_CHECK&#123; <span class="comment">#RS server健康检查部分</span></div><div class="line"> connect_timeout 10 <span class="comment">#定义超出10s连接超时</span></div><div class="line"> nb_get_retry 3 <span class="comment">#定义重试次数</span></div><div class="line"> delay_before_retry 3 <span class="comment">#定义重试时间间隔</span></div><div class="line"> connect_port 80 <span class="comment">#定义健康检查端口</span></div><div class="line"> &#125;</div><div class="line"> real_server 192.168.41.33 80 &#123; </div><div class="line"> weight 1</div><div class="line"> TCP_CHECK&#123; </div><div class="line"> connect_timeout 10 </div><div class="line"> nb_get_retry 3 </div><div class="line"> delay_before_retry 3 </div><div class="line"> connect_port 80 </div><div class="line"> &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="配置LVS-DR-BACKUP的keepalived-conf配置文件"><a href="#配置LVS-DR-BACKUP的keepalived-conf配置文件" class="headerlink" title="配置LVS-DR-BACKUP的keepalived.conf配置文件"></a>配置LVS-DR-BACKUP的keepalived.conf配置文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line">! Configuration File <span class="keyword">for</span> keepalived</div><div class="line">global_defs &#123;</div><div class="line"> notification_email &#123;</div><div class="line"> 752119102@qq.com <span class="comment">#设置报警邮箱，一般不再这做，而是用其他方式报警。</span></div><div class="line"> &#125;</div><div class="line"> notification_email_from keepalived@localhost <span class="comment">#设定发送邮件地址</span></div><div class="line"> smtp_server 127.0.0.1 <span class="comment">#设定发送邮件服务器</span></div><div class="line"> smtp_connect_timeout 30 <span class="comment">#设定SMTP连接超时时间</span></div><div class="line"> router_id LVS_DEVEL  <span class="comment">#负载均衡器标示，在局域网内是唯一的</span></div><div class="line">&#125;</div><div class="line">vrrp_instance VI_1 &#123; <span class="comment">#定义虚拟路由实例，不同实例ID不同。</span></div><div class="line"> state BACKUP  <span class="comment">#定义服务器在keepalived中的角色</span></div><div class="line"> interface eth0 <span class="comment">#定义进行检测的端口eth0</span></div><div class="line"> virtual_router_id 51 <span class="comment">#定义虚拟路由ID，同一个实例的主从一样。</span></div><div class="line"> priority 50  <span class="comment">#定义在虚拟路由器组的权限，越大越高</span></div><div class="line"> advert_int 1 <span class="comment">#定义检测时间间隔</span></div><div class="line"> authentication &#123; <span class="comment">#定义认证方式密码，主从必须一样</span></div><div class="line"> auth_type PASS</div><div class="line"> auth_pass 1111</div><div class="line"> &#125;</div><div class="line"> virtual_ipaddress &#123; <span class="comment">#指定虚拟IP地址</span></div><div class="line"> 192.168.41.249</div><div class="line"> &#125;</div><div class="line">&#125;</div><div class="line">virtual_server 192.168.41.249 80 &#123; <span class="comment">#定义虚拟服务，需指定IP地址和端口，空格隔开。</span></div><div class="line"> delay_loop 6 <span class="comment">#定义RS运行情况监测时间间隔</span></div><div class="line"> lb_algo rr <span class="comment">#定义负载调度算法</span></div><div class="line"> lb_kind DR <span class="comment">#定义LVS的工作模式</span></div><div class="line"> nat_mask 255.255.255.0 <span class="comment">#定义虚拟服务的mask</span></div><div class="line"> persistence_timeout 50 <span class="comment">#定义会话保持时间，S为单位</span></div><div class="line"> protocol TCP <span class="comment">#指定转发协议</span></div><div class="line"> real_server 192.168.41.31 80 &#123; <span class="comment">#定义真实服务器IP地址和端口</span></div><div class="line"> weight 1 <span class="comment">#定义RS的权重</span></div><div class="line"> TCP_CHECK&#123; <span class="comment">#RS server健康检查部分</span></div><div class="line"> connect_timeout 10 <span class="comment">#定义超出10s连接超时</span></div><div class="line"> nb_get_retry 3 <span class="comment">#定义重试次数</span></div><div class="line"> delay_before_retry 3 <span class="comment">#定义重试时间间隔</span></div><div class="line"> connect_port 80 <span class="comment">#定义健康检查端口</span></div><div class="line"> &#125;</div><div class="line"> real_server 192.168.41.33 80 &#123; </div><div class="line"> weight 1</div><div class="line"> TCP_CHECK&#123; </div><div class="line"> connect_timeout 10 </div><div class="line"> nb_get_retry 3 </div><div class="line"> delay_before_retry 3 </div><div class="line"> connect_port 80 </div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：这里主LVS-DR-MASTER和LVS-DR-BACKUP之间的配置的差别就只有红色部分：HA的角色（MASTER,BACKUP)和优先级不同，还有router_id。</p>
<h3 id="客户端配置LVS参数"><a href="#客户端配置LVS参数" class="headerlink" title="客户端配置LVS参数"></a>客户端配置LVS参数</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端需要做的工作就是绑定我们的VIP在lo口，并且进行ARP抑制，之前的文章已经提过此方法咯。现在我们就换成将配置写成脚本来执行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line">[root@RS2 ~]<span class="comment"># cat lvs-client.sh</span></div><div class="line"><span class="comment">#!/bin/bask</span></div><div class="line"><span class="comment"># 752119102@qq.com</span></div><div class="line"><span class="comment">#</span></div><div class="line">. /etc/rc.d/init.d/<span class="built_in">functions</span></div><div class="line">VIP=(</div><div class="line">192.168.41.249</div><div class="line">)</div><div class="line"><span class="keyword">function</span> <span class="function"><span class="title">start</span></span>()&#123;</div><div class="line"> <span class="keyword">for</span> ((i=0;i&lt;`<span class="built_in">echo</span> <span class="variable">$&#123;#VIP[*]&#125;</span>`;i++))</div><div class="line"> <span class="keyword">do</span></div><div class="line"> <span class="built_in">echo</span> <span class="variable">$&#123;i&#125;</span> <span class="variable">$&#123;VIP[$i]&#125;</span></div><div class="line"> ifconfig lo:<span class="variable">$&#123;i&#125;</span> <span class="variable">$&#123;VIP[$i]&#125;</span> netmask 255.255.255.255 up</div><div class="line"> route add -host <span class="variable">$&#123;VIP[$i]&#125;</span> dev lo</div><div class="line"> <span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"1"</span>&gt;/proc/sys/net/ipv4/conf/lo/arp_ignore</div><div class="line"><span class="built_in">echo</span> <span class="string">"2"</span>&gt;/proc/sys/net/ipv4/conf/lo/arp_announce</div><div class="line"><span class="built_in">echo</span> <span class="string">"1"</span>&gt;/proc/sys/net/ipv4/conf/all/arp_announce</div><div class="line"><span class="built_in">echo</span> <span class="string">"2"</span>&gt;/proc/sys/net/ipv4/conf/all/arp_announce</div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> <span class="function"><span class="title">stop</span></span>()&#123;</div><div class="line"> <span class="keyword">for</span> ((i=0;i&lt;<span class="variable">$&#123;#VIP[*]&#125;</span>;i++))</div><div class="line"> <span class="keyword">do</span></div><div class="line"> <span class="built_in">echo</span> <span class="variable">$&#123;i&#125;</span> <span class="variable">$&#123;VIP[$i]&#125;</span></div><div class="line"> ifconfig lo:<span class="variable">$&#123;i&#125;</span> <span class="variable">$&#123;VIP[$i]&#125;</span> netmask 255.255.255.255 up</div><div class="line"> route del -host <span class="variable">$&#123;VIP[$i]&#125;</span> dev lo:<span class="variable">$&#123;i&#125;</span></div><div class="line"> <span class="keyword">done</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line"> start)</div><div class="line"> start</div><div class="line"> <span class="built_in">exit</span></div><div class="line"> ;;</div><div class="line"> stop)</div><div class="line"> stop</div><div class="line"> <span class="built_in">exit</span></div><div class="line"> ;;</div><div class="line"> *)</div><div class="line"> <span class="built_in">echo</span> <span class="string">"You must use <span class="variable">$0</span>:stop|start"</span></div><div class="line"> ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<h3 id="测试实验结果"><a href="#测试实验结果" class="headerlink" title="测试实验结果"></a>测试实验结果</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果测试部成功可以按照三角的排查原理来进行排查，显示client到RS端是否能通讯，LB到RS能否通讯，client到LB是否能通讯，client到VIP是否能够通讯。并且查看LVS的运行状态。一定要确保keepalived.conf这个配置文件是正确的。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LVS/" rel="tag"># LVS</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/LVS/11. LVS原理详解及部署之四：keepalived介绍/" rel="next" title="LVS原理详解及部署之四：keepalived介绍">
                <i class="fa fa-chevron-left"></i> LVS原理详解及部署之四：keepalived介绍
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/LVS/13. LB 集群之 LVS 介绍/" rel="prev" title="LB 集群之 LVS 介绍">
                LB 集群之 LVS 介绍 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、实验环境需求-amp-准备"><span class="nav-number">1.</span> <span class="nav-text">一、实验环境需求&准备</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、配置keepalived实现负载均衡-amp-高可用"><span class="nav-number">2.</span> <span class="nav-text">二、配置keepalived实现负载均衡&高可用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装keepalived软件"><span class="nav-number">2.1.</span> <span class="nav-text">安装keepalived软件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置LVS-DR-MASK的keepalived-conf配置文件"><span class="nav-number">2.2.</span> <span class="nav-text">配置LVS-DR-MASK的keepalived.conf配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置LVS-DR-BACKUP的keepalived-conf配置文件"><span class="nav-number">2.3.</span> <span class="nav-text">配置LVS-DR-BACKUP的keepalived.conf配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#客户端配置LVS参数"><span class="nav-number">2.4.</span> <span class="nav-text">客户端配置LVS参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试实验结果"><span class="nav-number">2.5.</span> <span class="nav-text">测试实验结果</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
